package com.fpx.pc;

import java.text.DecimalFormat;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fpx.pc.data.web.service.API4UserMoney;
import com.fpx.pc.util.CheckUerUtils;
import com.fpx.pc.util.ExitApplication;

public class FpxMainActivity extends BaseActivity {
	/** Called when the activity is first created. */
	private int width;
	private int height;
	private int width4layoutTopBlank;
	private int height4layoutTopBlank;
	private int width4layoutLogoback;
	private int height4layoutLogoback;
	private int widthBtn;
	private int firstPadding;
	private CheckUerUtils checkUerUtils;
	private Context context;
	private ViewGroup.LayoutParams paramsLayoutFindway;
	private RelativeLayout layoutUserinfo;

	private Intent intent;
	/**
	 * 用户余额
	 */
	private String money;
	
	private boolean goLogin,saveUserFlag,savePasswordFlag;
	
	private ProgressDialog progressDialog = null;
	private TextView userinfo_aty,main_phone_call_layout_aty ;
	private Handler handler = null;
	private static final int MESSAGETYPE_02 = 0x0002;
	private static final int MESSAGETYPE_03 = 0x0003;
	private static final int MESSAGETYPE_04 = 0x0004;
	
	//再次改版 //2012年8月8日 星期三 15:29:01 pengcheng_liu
	private TextView _fpxmainlayout_content_linear_gjcx9jacky_aty,
    _fpxmainlayout_content_linear_yecxgjcx9jacky_aty,
    _fpxmainlayout_content_linear_yfssgjcx9jacky_aty,
    _fpxmainlayout_content_linear_yhdlgjcx9jacky_aty;

	// widthBtn=height4layoutTopBlank/2;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fpxmainlayout);
		// setContentView(R.layout.findwaylayout);
		LinearLayout layoutGlobal = (LinearLayout) findViewById(R.id.layoutGlobal);
//		layoutGlobal.addView(getLayoutTopBlank());
		LinearLayout layoutMiddle_H = (LinearLayout) getLayoutMiddle();
		LinearLayout layoutLogoback_V = (LinearLayout) getLayoutLogoback();
		LinearLayout layoutMiddle_V = (LinearLayout) getLayoutMiddleLeft();
		layoutLogoback_V.addView(getLayoutFindway());
		layoutLogoback_V.addView(getLayoutBalances());
		layoutLogoback_V.addView(getLayoutCount());
		layoutMiddle_H.addView(layoutMiddle_V);
		layoutMiddle_H.addView(layoutLogoback_V);
//		layoutGlobal.addView(layoutMiddle_H);
		
		
//		TextView _main_phone_call=(TextView) findViewById(R.id.main_phone_call);
//		_main_phone_call.setOnClickListener(new TxtPhone_Start_Onclick());
		
		//改版之后
		context = this;
		ExitApplication.getInstance().clearMemory(); 
		checkUerUtils = new CheckUerUtils(context);
		layoutUserinfo=(RelativeLayout) findViewById(R.id.main_userinfo_layout_id);
		userinfo_aty = (TextView) findViewById(R.id.txt_main_userinfo_id);
		main_phone_call_layout_aty = (TextView) findViewById(R.id.txt_main_phone_call_layout);
		
		main_phone_call_layout_aty.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				isExit();
			}
		});
		
		//2012年8月7日 星期二 17:14:06 pengcheng_liu  郭盛林建议取消拨打电话功能，经张工和徐工同意取消
//		LinearLayout _main_phone_call_layout=(LinearLayout) findViewById(R.id.main_phone_call_layout);
//		_main_phone_call_layout.setOnClickListener(new TxtPhone_Start_Onclick());
		
		checkLogin();
//		Intent intent = getIntent();
//		if (null!=intent) {
//			String username=intent.getStringExtra("userName");
//			if (null!=username&&!"".equals(username)) {
//				layoutUserinfo.setVisibility(View.VISIBLE);
//				userinfo_aty.setText(username);
//			}
//		}
		setLayoutUserinfoVisible();
		layoutUserinfo.setOnClickListener(new Layout_Logout_Onclick());
		
		
		GridView g = (GridView) findViewById(R.id.myGrid_login);
		g.setAdapter(new ImageAdapter(this));
		
		g.setOnItemClickListener(new ItemInner());
		
		//再次改版 //2012年8月8日 星期三 15:29:01 pengcheng_liu
		getView();
		
		
		_fpxmainlayout_content_linear_gjcx9jacky_aty.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.out.println("----16-144--");
				intent = new Intent();
				intent.setClass(FpxMainActivity.this, FindWayActivity.class);
				startActivity(intent);
			}
		});
		_fpxmainlayout_content_linear_yecxgjcx9jacky_aty.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.out.println("----16-余额信息--");
				intent = new Intent();
				intent.putExtra("title", "余额信息");
				intent.putExtra("inner1", "余额查询\n 您的余额为：￥19.00元整！\n 为了不影响您的使用，请及时充值！");
				if (!goLogin) {
						auto2BalanceInquiry();
//					}
				} else {
					//没有记住用户名，请先登录
					Toast.makeText(FpxMainActivity.this, "请先登录！", 5000).show();
					intent.putExtra("nextActivity", "BalanceInquiryActivity");
					intent.setClass(FpxMainActivity.this, LoginActivity.class);
					startActivity(intent);
				}
			}
		});
		_fpxmainlayout_content_linear_yfssgjcx9jacky_aty.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				intent = new Intent();
				intent.putExtra("title", "费用试算");
				intent.putExtra("inner1", "费用试算，请升级········");
				System.out.println("----16-费用试算177--");
				if (!goLogin) {
					intent.setClass(FpxMainActivity.this, CostCalculationActivity.class);
//					}
					//记住了直接到余额查询功能
				} else {
					//没有记住用户名，请先登录
					Toast.makeText(FpxMainActivity.this, "请先登录！", 5000).show();
					intent.putExtra("nextActivity", "CostCalculationActivity");
					intent.setClass(FpxMainActivity.this, LoginActivity.class);
				}
				startActivity(intent);
			}
		});
		_fpxmainlayout_content_linear_yhdlgjcx9jacky_aty.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				intent = new Intent();
				if (!goLogin) {
					//已经记住了用户名，到登出界面
					intent.setClass(FpxMainActivity.this, LogoutActivity.class);
				} else {
					//没有记住用户名，请先登录
//					Toast.makeText(FpxMainActivity.this, "请先登录！", 5000).show();
					intent.setClass(FpxMainActivity.this, LoginActivity.class);
				}
				startActivity(intent);
			}
		});
	}

	private int getScreenWidth() {
		// TODO Auto-generated method stub
		Display display1 = getDisplay();
		int screenWidth = display1.getWidth();
		return screenWidth;
	}

	private int getscreenHeight() {
		// TODO Auto-generated method stub
		Display display1 = getDisplay();
		int screenHeight = display1.getHeight();
		return screenHeight;
	}

	private Display getDisplay() {
		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		return display;
	}

	// 顶部留白的区域
	private View getLayoutTopBlank() {
		width = getScreenWidth();
		height = getscreenHeight();
		width4layoutTopBlank = width * 90 / 320;
		height4layoutTopBlank = height / 480 * 320;
		height4layoutTopBlank = (height - height4layoutTopBlank) / 5 * 2;
		// 顶部留白的区域
		ViewGroup.LayoutParams paramsLayoutTopBlank = new ViewGroup.LayoutParams(
				width4layoutTopBlank, height4layoutTopBlank);
		LinearLayout layoutTopBlank = new LinearLayout(this);
		layoutTopBlank.setLayoutParams(paramsLayoutTopBlank);
		layoutTopBlank.setOrientation(LinearLayout.VERTICAL);
		return layoutTopBlank;
	}

	// 中间布局,采用横向布局
	private View getLayoutMiddle() {
		// 中间布局
		ViewGroup.LayoutParams paramsLayoutMiddle = new ViewGroup.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		LinearLayout layoutMiddle = new LinearLayout(this);
		layoutMiddle.setOrientation(LinearLayout.HORIZONTAL);
		return layoutMiddle;
	}

	private View getLayoutMiddleLeft() {
		// 中间左侧留白区域
		int widthLayoutTopBlankLeft = (width - width4layoutLogoback) / 2;
		ViewGroup.LayoutParams paramsLayoutMiddleLeft = new ViewGroup.LayoutParams(
				widthLayoutTopBlankLeft, LayoutParams.WRAP_CONTENT);
		LinearLayout layoutMiddleLeft = new LinearLayout(this);
		layoutMiddleLeft.setLayoutParams(paramsLayoutMiddleLeft);
		layoutMiddleLeft.setOrientation(LinearLayout.VERTICAL);
		return layoutMiddleLeft;
	}

	private View getLayoutLogoback() {
		// 中间布局的中间操作区域
		width4layoutLogoback = width / 320 * 280;
		height4layoutLogoback = height / 480 * 320;
		ViewGroup.LayoutParams paramsLayoutLogoback = new ViewGroup.LayoutParams(
				width4layoutLogoback, height4layoutLogoback);
		// ViewGroup.MarginLayoutParams MarginparamsLayoutLogoback=new
		// ViewGroup.MarginLayoutParams(width4layoutTopBlank,LayoutParams.WRAP_CONTENT);
		LinearLayout layoutLogoback = new LinearLayout(this);
		layoutLogoback.setLayoutParams(paramsLayoutLogoback);
		// layoutLogoback.setPadding(38, 0, 0, 0);
		layoutLogoback.setGravity(Gravity.CENTER_HORIZONTAL);
		layoutLogoback.setBackgroundResource(R.drawable.logoback);
		layoutLogoback.setOrientation(LinearLayout.VERTICAL);
		return layoutLogoback;
	}

	// 轨迹查询
	private View getLayoutFindway() {
		widthBtn = width4layoutLogoback / 2;
		Button btnFindway = new Button(this);
		btnFindway.setWidth(widthBtn);
		btnFindway.setText("轨迹查询");

		btnFindway.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				intent = new Intent();
				intent.setClass(FpxMainActivity.this, FindWayActivity.class);
				startActivity(intent);
			}
		});

		// 轨迹查询
		int widthlayoutFindway = widthBtn;
		// firstPadding=80;
		firstPadding = height4layoutLogoback * 2 / 7;
		paramsLayoutFindway = new ViewGroup.LayoutParams(widthlayoutFindway,
				LayoutParams.WRAP_CONTENT);
		LinearLayout layoutFindway = new LinearLayout(this);
		layoutFindway.addView(btnFindway);
		layoutFindway.setPadding(0, firstPadding, 0, 0);
		layoutFindway.setLayoutParams(paramsLayoutFindway);
		return layoutFindway;
	}

	private View getLayoutBalances() {
		int otherPadding = 30;
		otherPadding = height4layoutLogoback / 30;

		// 余额查询按钮
		Button btnBalances = new Button(this);
		btnBalances.setWidth(widthBtn);
		btnBalances.setText("余额查询");

		btnBalances.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				intent = new Intent();
				intent.putExtra("title", "余额信息");
        		intent.putExtra("inner1", "余额查询\n 您的余额为：￥19.00元整！\n 为了不影响您的使用，请及时充值！");
				intent.setClass(FpxMainActivity.this, BalanceInquiryActivity.class);
				startActivity(intent);
			}
		});

		// 余额查询
		LinearLayout layoutBalances = new LinearLayout(this);
		layoutBalances.addView(btnBalances);
		layoutBalances.setPadding(0, otherPadding, 0, 0);
		layoutBalances.setLayoutParams(paramsLayoutFindway);
		return layoutBalances;
	}

	// 费用试算
	private View getLayoutCount() {
		int otherPadding = 30;
		otherPadding = height4layoutLogoback / 30;

		// 费用试算按钮
		Button btnCount = new Button(this);
		btnCount.setWidth(widthBtn);
		btnCount.setText("费用试算");
		
		btnCount.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				intent = new Intent();
				intent.putExtra("title", "费用试算");
				intent.putExtra("inner1", "费用试算，请升级········");
				intent.setClass(FpxMainActivity.this, CostCalculationActivity.class);
				startActivity(intent);
			}
		});

		// 费用试算
		LinearLayout layoutCount = new LinearLayout(this);
		layoutCount.addView(btnCount);
		layoutCount.setPadding(0, otherPadding, 0, 0);
		layoutCount.setLayoutParams(paramsLayoutFindway);
		// layoutLogoback.addView(layoutCount);
		return layoutCount;
	}
	
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//改版之后
	private class ImageAdapter extends BaseAdapter {
		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return mThumbIds.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		ImageView imageView;

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				imageView = new ImageView(mContext);

//				imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
				imageView.setLayoutParams(new GridView.LayoutParams(120,120));
				imageView.setAdjustViewBounds(false);
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				imageView.setPadding(25, 5, 5, 25);
			} else {
				imageView = (ImageView) convertView;
			}

			imageView.setImageResource(mThumbIds[position]);

			return imageView;
		}

		private Context mContext;

		private Integer[] mThumbIds = { R.drawable.gjcx9jacky, R.drawable.yecxgjcx9jacky,
				R.drawable.yfssgjcx9jacky, R.drawable.yhdlgjcx9jacky };
	}
	
	
	class ItemInner implements OnItemClickListener {

		public void onItemClick(AdapterView<?> arg0, View arg1, int viewID,
				long arg3) {
//			System.out.println("-------------->" + viewID + "---");
//			Toast.makeText(FpxMainActivity.this, "数据不能为空！"+viewID, 5000).show();
			switch(viewID){
			case 0:
				intent = new Intent();
				intent.setClass(FpxMainActivity.this, FindWayActivity.class);
				startActivity(intent);
				break;
			case 1:
				intent = new Intent();
				intent.putExtra("title", "余额信息");
        		intent.putExtra("inner1", "余额查询\n 您的余额为：￥19.00元整！\n 为了不影响您的使用，请及时充值！");
        		if (!goLogin) {
        			//记住了直接到余额查询功能
//        			intent.setClass(FpxMainActivity.this, BalanceInquiryActivity.class);
//        			if (saveUserFlag) {
        				auto2BalanceInquiry();
//					}
				} else {
					//没有记住用户名，请先登录
					Toast.makeText(FpxMainActivity.this, "请先登录！", 5000).show();
					intent.putExtra("nextActivity", "BalanceInquiryActivity");
					intent.setClass(FpxMainActivity.this, LoginActivity.class);
					startActivity(intent);
				}
				break;
			case 2:
				intent = new Intent();
				intent.putExtra("title", "费用试算");
				intent.putExtra("inner1", "费用试算，请升级········");
				if (!goLogin) {
//					if (saveUserFlag) {
//						CostCalculation2LayoutActivity
					intent.setClass(FpxMainActivity.this, CostCalculationActivity.class);
//						intent.setClass(FpxMainActivity.this, CostCalculation2LayoutActivity.class);
//					}
					//记住了直接到余额查询功能
				} else {
					//没有记住用户名，请先登录
					Toast.makeText(FpxMainActivity.this, "请先登录！", 5000).show();
					intent.putExtra("nextActivity", "CostCalculationActivity");
					intent.setClass(FpxMainActivity.this, LoginActivity.class);
				}
				startActivity(intent);
				break;
			case 3:
				intent = new Intent();
				if (!goLogin) {
        			//已经记住了用户名，到登出界面
        			intent.setClass(FpxMainActivity.this, LogoutActivity.class);
				} else {
					//没有记住用户名，请先登录
//					Toast.makeText(FpxMainActivity.this, "请先登录！", 5000).show();
					intent.setClass(FpxMainActivity.this, LoginActivity.class);
				}
				startActivity(intent);
				break;
			}
//			finish();
		}

	}
	
	/**
	 * 检查是否已经记住了用户名
	 */
	private void checkLogin() {
		// TODO Auto-generated method stub
//		CheckUerUtils checkUerUtils=new CheckUerUtils(FpxMainActivity.this);
		goLogin=checkUerUtils.isSaveUserInfo1();
//		goLogin=checkUerUtils.isSaveUserInfo();
//		boolean flag1=checkUerUtils.isSaveUserInfo1();
//		boolean flag=checkUerUtils.isSaveUserInfo();
//		System.out.println("fpxMainActivity--401-"+flag1+"--"+flag);
//		goLogin=!checkUerUtils.isSaveUserInfo1()?!checkUerUtils.isSaveUserInfo1():checkUerUtils.isSaveUserInfo();
//		String check_user=checkUerUtils.getUserName();
//		String check_passwrod=checkUerUtils.getPassWord();
//		if (null==check_passwrod||"".equals(check_passwrod)||null==check_user||"".equals(check_user)) {
//			saveUserFlag=false;
//		}else{
//			saveUserFlag=true;
//		}
	}

	
	/**
	 * 直接拨打电话
	 */
	class TxtPhone_Call_Onclick implements  OnClickListener{
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String phone=getResources().getString(R.string.STRtxt_main_phone);
			//直接拨打
			Intent intent = new Intent();
			
			//系统默认的action，用来打开默认的电话界面
			intent.setAction(Intent.ACTION_CALL);
			
			//需要拨打的号码
			intent.setData(Uri.parse("tel:"+phone));
			startActivity(intent);
//			finish();
		}
	}
	
	/**
	 * 调用拨打电话的界面
	 * @author luandly
	 *
	 */
	class TxtPhone_Start_Onclick implements  OnClickListener{

		@Override
		public void onClick(View v) {
			String phone=getResources().getString(R.string.STRtxt_main_phone);
			// TODO Auto-generated method stub
			//直接拨打
		    Intent intent = new Intent();

		  //系统默认的action，用来打开默认的电话界面
		  intent.setAction(Intent.ACTION_DIAL);
		  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
		  //需要拨打的号码
		  
		  intent.setData(Uri.parse("tel:"+phone));
		  startActivity(intent);
//		  finish();

		}
	}

	
	private void auto2BalanceInquiry(){
		String progressDialogTitle=getResources().getString(R.string.str_progressDialogTitle); 
		String progressDialogInner=getResources().getString(R.string.str_progressDialogInner); 
		progressDialog = ProgressDialog.show(FpxMainActivity.this,progressDialogTitle, progressDialogInner);
//		final Message msg_listData = new Message();

		new Thread() {
			public void run() {
				try {
					// 连接网络获取数据
//					CheckUerUtils checkUerUtils;
//					checkUerUtils = new CheckUerUtils(FpxMainActivity.this);
					String name1 = checkUerUtils.getUserName1();
					String password1 = checkUerUtils.getPassWord1();
					String name2 = checkUerUtils.getUserName();
					String password2 = checkUerUtils.getPassWord();
					
					 API4UserMoney api4UserMoney = new API4UserMoney();
					// 2012年6月28日18:37:59 pengcheng_liu
					money = api4UserMoney.getMoney4px9URL(name1, password1);
					String result="网络获取超时！";
					Intent intent = new Intent();
					if ("".equals(money) || null == money) {
						intent.putExtra("inner1", "没有查询到相关信息，详情请咨询客服！");
						Message msg_listData = new Message();
						msg_listData.what = MESSAGETYPE_04;
						handler.sendMessage(msg_listData);
						return;
					}else  if(result.equals(money)||result==money){
						intent.putExtra("inner1", result);
						Message msg_listData = new Message();
						msg_listData.what = MESSAGETYPE_03;
						handler.sendMessage(msg_listData);
						return;
					}
					else  {
						//2012年7月19日21:17:56 pengcheng_liu
						
						//2012年7月26日11:57:03 pengcheng_liu start
//						8.余额查询界面显示内容较少，字且较小，界面很大。不协调（做成弹出框形式）
//						更改为千分位计数
//						double temp=Double.parseDouble(money);
//						String moneys = DecimalFormat.getNumberInstance().format(temp);
//						System.out.println(moneys+"--9---"+money);
//						intent.putExtra("inner1", " 您个人账户余额为：" + moneys+" 元");
////						// 记住了直接到余额查询功能
//						intent.putExtra("title", "余额信息");
//						intent.putExtra("userinfo", "余额信息");
//						intent.setClass(FpxMainActivity.this,BalanceInquiryActivity.class);
//						startActivity(intent);
//						finish();
						//2012年7月26日11:57:03 pengcheng_liu end
						Message msg_listData = new Message();
						msg_listData.what = MESSAGETYPE_02;
						handler.sendMessage(msg_listData);
					}
				} catch (Exception e) {

				}
			}
		}.start();

		handler = new Handler() {
			public void handleMessage(Message message) {
				switch (message.what) {
				case MESSAGETYPE_02:
					// 刷新UI，显示数据，并关闭进度条
					progressDialog.dismiss(); // 关闭进度条
					//2012年7月26日11:57:03 pengcheng_liu end
					showDialog(DIALOG_USER_MONEY);
					break;
				case MESSAGETYPE_03:
					progressDialog.dismiss(); 
//					Toast.makeText(LoginActivity.this, "网络获取超时！", 5000).show();
//					clearTxt();
					showDialog(DIALOG_LOGIN_NO_NETWORK);
					break;
				case MESSAGETYPE_04:
					progressDialog.dismiss(); 
//					Toast.makeText(LoginActivity.this, "用户名或密码不正确，请重新输入！", 5000).show();
//					clearTxt();
					showDialog(DIALOG_LOGIN_NO_USER);
					break;
				}
			}
		};
	}
	
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_USER_MONEY:
			double temp=Double.parseDouble(money);
			String moneys = DecimalFormat.getNumberInstance().format(temp);
			String moneyinfo="您的个人账户\n余额为：\n"+moneys+" 元";
			return new AlertDialog.Builder(FpxMainActivity.this)
			.setTitle(R.string.alert_dialog_user_money_title)
			.setMessage(moneyinfo)
			.setPositiveButton(R.string.alert_dialog_ok,
					new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
						int whichButton) {
					dialog.cancel();
					removeDialog(DIALOG_USER_MONEY);
//					finish();
				}
			}).create();
		case DIALOG_LOGIN_NO_NETWORK:
			return new AlertDialog.Builder(FpxMainActivity.this)
			.setMessage(R.string.alert_dialog_no_network)
			.setPositiveButton(R.string.alert_dialog_ok,
					new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
						int whichButton) {
					EditText _login_username_id_ = (EditText) findViewById(R.id.login_username_id_add);
					EditText _login_password_ = (EditText) findViewById(R.id.login_password_add);
//					出异常暂时取消//2012年7月17日9:22:07 pengcheng_liu
//					_login_username_id_.setText("");
//					_login_password_.setText("");
//					 CheckUerUtils checkUerUtils = new CheckUerUtils(BaseActivity.this);
//					 checkUerUtils.deleteUserDate();
//					_login_username_id_.setFocusable(true);
//					_login_username_id_.setFocusableInTouchMode(true);
//					_login_username_id_.requestFocus();
					removeDialog(DIALOG_LOGIN_NO_NETWORK);
				}
			}).create();
		case DIALOG_LOGIN_NO_USER:
			return new AlertDialog.Builder(FpxMainActivity.this)
			.setMessage(R.string.alert_dialog_no_user)
			.setPositiveButton(R.string.alert_dialog_ok,
					new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
						int whichButton) {
					EditText _login_username_id_ = (EditText) findViewById(R.id.login_username_id_add);
					EditText _login_password_ = (EditText) findViewById(R.id.login_password_add);
//					_login_username_id_.setText("");
//					_login_password_.setText("");
					 CheckUerUtils checkUerUtils = new CheckUerUtils(FpxMainActivity.this);
					 checkUerUtils.deleteUserDate();
					_login_username_id_.setFocusable(true);
					_login_username_id_.setFocusableInTouchMode(true);
					_login_username_id_.requestFocus();
					removeDialog(DIALOG_LOGIN_NO_USER);
				}
			}).create();
		}
		return null;
	}
	
	
	class Layout_Logout_Onclick implements  OnClickListener{
		
		@Override
		public void onClick(View v) {
			checkUerUtils.deleteUserDate_1();
			checkUerUtils.deleteUserDate();
			layoutUserinfo.setVisibility(View.GONE);
			intent=new Intent();
			intent.setClass(FpxMainActivity.this, LoginActivity.class);
			startActivity(intent);
			finish();
			
		}
	}
	
	public void isExit(){
    	new AlertDialog.Builder(context).setTitle("确定退出吗?")
        .setNeutralButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//            	checkUerUtils=new CheckUerUtils(activity);
                checkUerUtils.deleteUserDate_1();
                
                Intent home = new Intent(Intent.ACTION_MAIN);
                home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
		        home.addCategory(Intent.CATEGORY_HOME);   
//    			
//    			intent.setAction(Intent.ACTION_MAIN);
    			startActivity(home);
            	finish();
            	ExitApplication.getInstance().exit(); 
            }
        })
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        }).show();
    }
    public void onBackPressed() {
    	isExit();
//    	finish();
	}
    
    public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {//KeyEvent.KEYCODE_BACK即是back键对应的code
	//	    Toast.makeText(this, "Back key pressed!", Toast.LENGTH_SHORT).show();//当back键按下的时候会弹出Toast提示，你也可以将上一示例的对话框加入下来
	//	    return true;//返回true表示该事件已被处理，不会再传递到次级监听者;返回false表示该事件还没有被处理完，会继续传递给其他监听者
			isExit();
	//		finish();
		}
		return false;//如果按下的是其他按键则直接返回false表示该事件没有处理。
    } 

    /**
     * 控制用户信息是否显示，因为后面的登录操作，只要登录就记住了用户名和密码
     * 所以只要检查"一次性登录"的是否为真就可以了！同时获取用户名和密码就好了
     */
    private void setLayoutUserinfoVisible() {
		// TODO Auto-generated method stub
    	checkUerUtils=new CheckUerUtils(context);
    	String name="";
    	String password="";
    	boolean flag1=checkUerUtils.isSaveUserInfo1();
    	boolean flag=checkUerUtils.isSaveUserInfo();
    	boolean flag2=checkUerUtils.isAutoLogin();
    	if (!flag1) {
//    		name=checkUerUtils.getUserName1();
    		//2012年8月9日 星期四 14:12:27 pengcheng_liu  显示客户编号
    		name=checkUerUtils.getCompanyCode();
//    		System.out.println("FpxMainActivity--776--"+name);
			userinfo_aty.setText(name);
			layoutUserinfo.setVisibility(View.VISIBLE);
		}
    	//当在用户登录界面记住了用户信息，或自动登录了，一样的要在主界面显示用户信息，同时更新临时用户信息
    	if (!flag||!flag2) {
    		name=checkUerUtils.getUserName();
    		password=checkUerUtils.getPassWord();
    		userinfo_aty.setText(name);
    		layoutUserinfo.setVisibility(View.VISIBLE);
    		checkUerUtils.saveUserDate_1(name,password);
    		goLogin=false;
		}
//    	System.out.println("fpxMainActivity--622-"+name+"--"+flag);
	}
    
    private void getView() {
		// TODO Auto-generated method stub
    	_fpxmainlayout_content_linear_gjcx9jacky_aty = (TextView) findViewById(R.id.fpxmainlayout_content_linear_gjcx9jacky);
    	_fpxmainlayout_content_linear_yecxgjcx9jacky_aty = (TextView) findViewById(R.id.fpxmainlayout_content_linear_yecxgjcx9jacky);
    	_fpxmainlayout_content_linear_yfssgjcx9jacky_aty = (TextView) findViewById(R.id.fpxmainlayout_content_linear_yfssgjcx9jacky);
    	_fpxmainlayout_content_linear_yhdlgjcx9jacky_aty = (TextView) findViewById(R.id.fpxmainlayout_content_linear_yhdlgjcx9jacky);
	}

}